import os
from sys import argv


if len(argv) != 3:
	print "uso: python", argv[0], "nome_do_skeleton_a_ser_dividido.java package_dos_arquvivos_java"
	exit()
	
with open(argv[1], "r") as f:
	if not os.access("parserJavaFiles", os.F_OK):
		os.mkdir("parserJavaFiles")
	os.chdir("parserJavaFiles")
	n = 1
	while True:
		line = f.readline()
		stopString = "AINDA NAO SEI"
		while stopString == "AINDA NAO SEI":
			print line
			if line.strip() == "//RULE HANDLER CLASS BEGINS ===============================================":
				stopString = "//=========================================================================\n"
			if line.strip() == "// MAIN PROGRAM SHELL BEGINS ==============================================":
				stopString = ""
			line = f.readline()
		while line[:3] == "//=":
			line = f.readline()

		# Constroi um arquivo:
		line = f.readline()
		name = "out.java"
		with open("tmp.java", "w") as ofile:
			#ofile.write("package "+argv[2]+";\n\n")
			while line != stopString:
				ofile.write(line)
				#print line,
				if line.split()[:2] == "public class".split():
					name = line.split()[2] + ".java"
					print name
				line = f.readline()
		print name
		os.rename("tmp.java", name)
		
		print n
		n+=1
		print stopString
		if stopString == "":
			exit()